using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentContactTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentContactTable objects.
    /// </summary>
    public class StudentContactTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentContactTable from the underlying datasource.
        /// </summary>
        /// <param name="studentContactKey">The unique StudentContactKey of the StudentContactTable in the database.</param>
        /// <returns>An StudentContactTable when the StudentContactKey was found in the database, or null otherwise.</returns>
        public static StudentContactTable GetItem(int studentContactKey)
        {
            StudentContactTable myStudentContactTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentContactTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@studentContactKey", studentContactKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentContactTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentContactTable;
            }
        }


        /// <summary>
        /// Gets an instance of StudentContactTable from the underlying datasource.
        /// </summary>
        /// <param name="UserAccountKey">The unique UserAccountKey of the StudentContactTable in the database.</param>
        /// <returns>An StudentContactTable when the UserAccountKey was found in the database, or null otherwise.</returns>
        public static StudentContactTable GetItemByUserAccountKey(int UserAccountKey)
        {
            StudentContactTable myStudentContactTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentContactTableSelectSingleItemByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentContactTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentContactTable;
            }
        }

        /// <summary>
        /// Returns a list with StudentContactTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentContactTable objects.</returns>
        public static StudentContactTableList GetList()
        {
            StudentContactTableList tempList = new StudentContactTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentContactTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Returns a list with StudentContactTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentContactTable objects.</returns>
        public static StudentContactTableList GetListByUserAccountKey(int UserAccountKey)
        {
            StudentContactTableList tempList = new StudentContactTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentContactTableSelectListByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
                return tempList;
            }
        }

        /// <summary>
        /// Saves a StudentContactTable in the database.
        /// </summary>
        /// <param name="myStudentContactTable">The StudentContactTable instance to save.</param>
        /// <returns>The new StudentContactKey if the StudentContactTable is new in the database or the existing StudentContactKey when an item was updated.</returns>
        public static int Save(StudentContactTable myStudentContactTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentContactTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentContactTable.StudentContactKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@studentContactKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@studentContactKey", myStudentContactTable.StudentContactKey);
                    }
                    if (myStudentContactTable.UserAccountKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", myStudentContactTable.UserAccountKey);
                    }
                    if (string.IsNullOrEmpty(myStudentContactTable.MobileNumber))
                    {
                        myCommand.Parameters.AddWithValue("@mobileNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@mobileNumber", myStudentContactTable.MobileNumber);
                    }
                    if (string.IsNullOrEmpty(myStudentContactTable.HomePhoneNumber))
                    {
                        myCommand.Parameters.AddWithValue("@homePhoneNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@homePhoneNumber", myStudentContactTable.HomePhoneNumber);
                    }
                    if (string.IsNullOrEmpty(myStudentContactTable.FaxNumber))
                    {
                        myCommand.Parameters.AddWithValue("@FaxNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@FaxNumber", myStudentContactTable.FaxNumber);
                    }
                    if (string.IsNullOrEmpty(myStudentContactTable.OfficePhoneNumber))
                    {
                        myCommand.Parameters.AddWithValue("@OfficePhoneNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@OfficePhoneNumber", myStudentContactTable.OfficePhoneNumber);
                    }
                    if (string.IsNullOrEmpty(myStudentContactTable.OtherNumber))
                    {
                        myCommand.Parameters.AddWithValue("@otherNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@otherNumber", myStudentContactTable.OtherNumber);
                    }
                    if (string.IsNullOrEmpty(myStudentContactTable.EmailAddress))
                    {
                        myCommand.Parameters.AddWithValue("@emailAddress", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@emailAddress", myStudentContactTable.EmailAddress);
                    }

                    myCommand.Parameters.AddWithValue("@status", myStudentContactTable.Status);

                    //if (myStudentContactTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myStudentContactTable.CreatedDateTime);
                    //}
                    //if (myStudentContactTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentContactTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentContactTable from the database.
        /// </summary>
        /// <param name="studentContactKey">The StudentContactKey of the StudentContactTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int studentContactKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentContactTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@studentContactKey", studentContactKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentContactTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentContactTable FillDataRecord(IDataRecord myDataRecord)
        {
            StudentContactTable myStudentContactTable = new StudentContactTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StudentContactKey")))
            {
                myStudentContactTable.StudentContactKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StudentContactKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
            {
                myStudentContactTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MobileNumber")))
            {
                myStudentContactTable.MobileNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("MobileNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("HomePhoneNumber")))
            {
                myStudentContactTable.HomePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("HomePhoneNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FaxNumber")))
            {
                myStudentContactTable.FaxNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("FaxNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OfficePhoneNumber")))
            {
                myStudentContactTable.OfficePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OfficePhoneNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherNumber")))
            {
                myStudentContactTable.OtherNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmailAddress")))
            {
                myStudentContactTable.EmailAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("EmailAddress"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentContactTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentContactTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentContactTable;
        }
    }

}
